import router from './router'
import { userStore } from '@/pinia/modules/user'
import { getUrlParams, loadScript } from "@/utils/public";
import { setItem } from '@/utils/storage';
let getgetActInfoSet = false
router.beforeEach( async (to, from) => {
  const useUserStore = userStore();
  let {
    user_id = getUrlParams("user_id") || useUserStore.user_id,
    custom_id = getUrlParams("custom_id") || useUserStore.custom_id,
    u_openid = getUrlParams("u_openid") || useUserStore.u_openid,
    pageType = getUrlParams("pageType") || useUserStore.pageType
  } = to.query
  console.log('user_id', user_id);
  console.log('u_openid', u_openid);
  if(!user_id || !custom_id) {
    if( to.path!='/404') {
      return {
        path:'/404'
      }
    }
  }
  if(!getgetActInfoSet) {
    getgetActInfoSet = true
    useUserStore.pageType = Number(pageType)
    useUserStore.user_id = user_id;
    useUserStore.u_openid = u_openid;
    useUserStore.custom_id = custom_id;
    setItem('user_id',user_id)
    setItem('u_openid',u_openid)
    setItem('custom_id',custom_id)
    setItem('pageType',pageType)
    useUserStore.room_id = getUrlParams("room_id") || ''
    const { css } = await useUserStore.getActInfo()
    console.log(css);
    // const modules = import.meta.glob('../public/scss/*.css',{query:'css'})
    // console.log(modules[`../public/scss/${css}`]);
    // modules[`../public/scss/${css}`]()
    loadScript(`./css/${css}`)
    await useUserStore.getSystemInfo()
  }
  return true
})

router.afterEach((to) => {
  document.title = to.meta.title as string;
})
